package com.htstar.ovms.device.controller;

import com.htstar.ovms.common.core.util.R;
import com.htstar.ovms.device.api.constant.RocketMQConstant;
import com.htstar.ovms.device.api.feign.DeviceGatewayFeign;
import com.htstar.ovms.device.api.wrapper.MessageWrapper;
import com.htstar.ovms.device.api.wrapper.OTAData;
import com.htstar.ovms.device.service.OTAUpdateService;
import com.htstar.ovms.device.service.RocketMqSenderService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/OTAUpdata")
public class OTAUpdateController {

    @Autowired
    DeviceGatewayFeign deviceGatewayFeign;

    @Autowired
    OTAUpdateService otaUpdateService;

    @Autowired
    RocketMqSenderService rocketMqSenderService;

    @ApiOperation(value = "单个下发指令/Single instruction", notes = "单个下发指令")
    @PostMapping("update")
    public R OTA(@RequestBody OTAData req) throws Exception {
        return deviceGatewayFeign.OTA(req);
    }

    @ApiOperation(value = "多个下发指令/Multiple issued instructions", notes = "多个下发指令")
    @PostMapping("updateBatch")
    public R updateBatch(@RequestBody OTAData req) throws Exception {
        R rs = otaUpdateService.updateBatch(req);
        return rs;
    }


    public static byte[] hexStrToByteArray(String str) {
        if (str == null) {
            return null;
        }
        if (str.length() == 0) {
            return new byte[0];
        }
        byte[] byteArray = new byte[str.length() / 2];
        for (int i = 0; i < byteArray.length; i++) {
            String subStr = str.substring(2 * i, 2 * i + 2);
            byteArray[i] = ((byte) Integer.parseInt(subStr, 16));
        }
        return byteArray;
    }

}
